package com.kaikeba.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.UserService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserController {
    @ResponseBody(value = "/admin/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> map = UserService.console();
        Message msg = new Message();
        if(map == null || map.isEmpty()){
            msg.setStatus(-1);
        }else{
            msg.setStatus(1);
        }
        msg.setObject(map);
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/admin/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        String offset = req.getParameter("offset");
        String pageNumber = req.getParameter("pageNumber");
        List<User> list = UserService.findAll(1, Integer.parseInt(offset), Integer.parseInt(pageNumber));
        List<BootstrapTableUser> bList = new ArrayList<>();
        ResultList<BootstrapTableUser> resultList = new ResultList<>();
        for (User user : list) {
            bList.add(userToBTE(user));
        }
        resultList.setRows(bList);
        resultList.setTotal(UserService.console().get("sum"));
        return JSONUtil.toJson(resultList);
    }

    @ResponseBody(value = "/admin/user/add.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPhone(phone);
        user.setPassword(password);
        boolean r = UserService.insert(user);
        Message msg = new Message();
        if(r){
            msg.setStatus(1);
            msg.setResult("增加成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("增加失败");
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/admin/user/findByPhone.do")
    public String findByPhone(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        User man = UserService.findByPhone(phone);
        Message msg = new Message();
        if(man == null){
            msg.setStatus(-1);
            msg.setResult("用户不存在");
        }else{
            msg.setStatus(1);
            msg.setResult("查找成功");
        }
        msg.setObject(man);
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/admin/user/deleteByPhone.do")
    public String deleteByPhone(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        boolean r = UserService.deleteByPhone(phone);
        Message msg = new Message();
        if(r){
            msg.setStatus(1);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody("/admin/user/updateByPhone.do")
    public String updateByPhone(HttpServletRequest req, HttpServletResponse resp){
        String oldPhone = req.getParameter("oldPhone");
        String newPhone = req.getParameter("newPhone");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setPhone(newPhone);
        user.setUsername(username);
        user.setPassword(password);
        boolean r = UserService.updateByPhone(oldPhone, user);
        Message msg = new Message();
        if(r){
            msg.setStatus(1);
            msg.setResult("更新成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("更新失败");
        }
        return JSONUtil.toJson(msg);
    }

    private BootstrapTableUser userToBTE(User user){
        BootstrapTableUser bte = new BootstrapTableUser();
        bte.setId(user.getId());
        bte.setUsername(user.getUsername());
        bte.setPhone(user.getPhone());
        bte.setPassword(user.getPassword());
        bte.setRegisterTime(DateFormatUtil.format(user.getRegisterTime()));
        if(user.getLastLoginTime() == null){
            bte.setLastLoginTime("未登录");
        }else{
            bte.setLastLoginTime(DateFormatUtil.format(user.getLastLoginTime()));
        }
        return bte;
    }
}
